package com.ztmg.water.service.student.impl;

import com.ztmg.water.model.student.Student;
import com.ztmg.water.vo.student.StudentVO;
import com.ztmg.water.dto.student.StudentDTO;
import com.ztmg.water.dto.student.StudentPageDTO;
import com.ztmg.common.util.resultUtils.JsonResult;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztmg.common.constant.ErrorConst;
import org.springframework.beans.BeanUtils;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import com.ztmg.water.service.student.StudentService;
import com.ztmg.water.mapper.student.StudentMapper;import com.ztmg.water.model.student.StudentExample;

@Service
public class StudentServiceImpl implements StudentService{

    @Resource
    private StudentMapper studentMapper;


    @Override
    public JsonResult selectStudentNumsByParams(StudentPageDTO dto) {
        Map<String, String> params = new HashMap<>(2);
        int add = studentMapper.selectCountStudentByParams(params);

            params.put("isNewAdd","1");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            params.put("minTime",sdf.format(calendar.getTime()));
            params.put("maxTime",sdf.format(new Date()));
        int total = studentMapper.selectCountStudentByParams(params);
        Map<String,Object> result = new HashMap<>(2);
        result.put("addNums",add);
        result.put("totalNums",total);

        return JsonResult.success(result);
    }

    @Override
    public JsonResult create(StudentDTO dto) {
        if(null != dto){
            Student bean = new Student();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreateTime(new Date());
            //后续完善
            bean.setCreater("");
            studentMapper.insert(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult update(StudentDTO dto) {
        if(null != dto && null != dto.getId()){
            Student bean = new Student();
            BeanUtils.copyProperties(dto,bean);
            bean.setModifyTime(new Date());
            //后续完善
            bean.setModifier("");
            studentMapper.updateByPrimaryKeySelective(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<StudentVO> selectById(Integer id) {
        if(null != id){
            Student bean = studentMapper.selectByPrimaryKey(id);
            StudentVO vo = new StudentVO();
            if(null != bean){
                BeanUtils.copyProperties(bean,vo);
            }else {
                return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
            }
            return JsonResult.success(vo);
        }else{
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<StudentVO> select(StudentDTO dto) {
        if(dto.getId()==0)
        {
            dto.setId(null);
        };
        StudentExample example = new StudentExample();
        StudentExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        List<Student> sysUserList = studentMapper.selectByExample(example);
        StudentVO vo = new StudentVO();
        if(!sysUserList.isEmpty()){
            BeanUtils.copyProperties(sysUserList.get(0),vo);
        }
        return JsonResult.success(vo);
    }

    @Override
    public JsonResult<PageInfo<StudentVO>> selectPage(StudentPageDTO dto) {
        StudentExample example = new StudentExample();
        StudentExample.Criteria criteria =  example.createCriteria();
        if(!dto.getIsNewAdd().equals(""))
        {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            criteria.andCreateTimeBetween(calendar.getTime(),new Date());
        }
        if(!dto.getSearchKey().equals(""))
        {
            criteria.andStudentSnLike(dto.getSearchKey());
            criteria.andStudentNameLike(dto.getSearchKey());
        }
        /***
        * 根据具体的业务，查询条件赋值
         */
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<Student> beanList = studentMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(beanList);
        List<StudentVO> respDTOS = beanList.parallelStream().map((bean) -> {
            StudentVO vo = new StudentVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        pageInfo.setList(respDTOS);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult deleteById(Integer id) {
        return JsonResult.success( studentMapper.deleteByPrimaryKey(id));
    }

    @Override
    public JsonResult delete(StudentDTO dto) {
        StudentExample example = new StudentExample();
        StudentExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        return JsonResult.success(studentMapper.deleteByExample(example));
    }

}